<?
include_once("/home/gru/public_html_dev/lib/common.php");
include_once("/home/gru/public_html_dev/lib/cDB.php");
include_once("/home/gru/public_html_dev/lib/cDAO.php");


$db= new cDB();
$dao = new cDAO($db->getCon());

//-----------------------------------------------------------------------------
// 쿠폰 카테고리코드 업데이트
//-----------------------------------------------------------------------------
$opt = "";
$q  = " SELECT * FROM coupon $opt";

$arr = $dao->getSheet($q);
if($arr==NULL)exit;

$q = "select * from _good_code";
$arrGoodCode = $dao->getSheet($q);

foreach($arr as $i=>$v){
	$id		= $v['id'];
	$pid	= $v['pid'];
	$domain	= $v['domain'];
	$area1	= $v['area1'];
	$area2	= $v['area2'];
	$name	= $v['name'];
	
	$category	= $v['category'];
		
	$description	= $v['description'];
	$shop_name	= $v['shop_name'];	
	
		
	// 지정된 상품 분류 코드로 설정
	$key = explode("/", $category);

	$categoryCode = 0;
	//수집한 카테고리 정보로 검색
	foreach($key as $v2){
		if($v2 == "")break;//정보가 없다면 loop나감
		foreach($arrGoodCode as $g){
			$arrCat = explode("/", $g['m5_bkbn']);
			if(in_array($v2,$arrCat)){			

				if($categoryCode == 0){
					$categoryCode = $g['m5_code'];
					break;
				}					
			}
		}	
	}
	
	//카테고리가 없으면 제목을 바탕으로 다시 검색
	if($categoryCode == 0){
		
		foreach($arrGoodCode as $gc){

			$arrCat = explode("/", $gc['m5_bkbn']);
			
			foreach($arrCat as $ac){
				
				if(strstr($name,$ac) != FALSE){
					
					$categoryCode = $gc['m5_code'];
					break;
				}
			}
			
			if($categoryCode != 0)break;
			
		}	
		
	}
	
	//수집된 카테고리, 제목에서도 못찾았으면 '기타' 처리 99
	if($categoryCode == 0)$categoryCode = 99;
	
	
	
	$q = " UPDATE coupon SET categoryCode = $categoryCode WHERE id = $id";
	debug_flush($q);
	$dao->query($q);
	
}

$db->close();
?>
